package com.productline;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import org.jdom.Document;
import org.jdom.Element;

public class Generator {
	
	public static void main(String[] argv){
		
		GeneratorFrame f = new GeneratorFrame();
		f.setVisible(true);
	}
	
//	public static void saveConfiguration(Document doc){
//		
//	}
	
	public static void generate(Document doc){
		Sensor[] sensors;
		int transmitPeriod;
		int history;
		String messageFormat;
		int sensorNumber = 0;
		
		Element root = doc.getRootElement();
		
		List<Element> sensorList = root.getChild("sensors").getChildren();
		int sensorsNum = sensorList.size();
		sensors = new Sensor[sensorsNum];
		for(int i = 0; i < sensorsNum; i++){
			Element e = sensorList.get(i);
			sensors[i] = new Sensor(e.getChildText("name"),
					Integer.valueOf(e.getChildText("number")),
					Integer.valueOf(e.getChildText("weight")),
					Integer.valueOf(e.getChildText("period"))*1000);
			sensorNumber += sensors[i].number;
		}
		transmitPeriod = Integer.valueOf(root.getChildText("transmitPeriod"));
		history = Integer.valueOf(root.getChildText("history"));
		messageFormat = root.getChildText("messageFormat");
		
		
		File file = new File(".\\Generator\\build/FWS.java");
		PrintWriter writer = null;
		try {
			writer = new PrintWriter(new FileWriter(file, false));
		} catch (IOException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}

		//Read FWS0.java
		appendFile(".\\Generator\\codeBase/FWS0.java", writer);
		
		//Generate constant
		String temp = "public static final Sensor[] sensors = {";
		for (int i = 0; i < sensors.length; i++){
			temp += "new Sensor( \"" +
						sensors[i].name + "\", " +
						sensors[i].number + ", " +
						sensors[i].weight + ", " +
						sensors[i].period + ")";
			if (i != sensors.length -1)
				temp += ", \n";
			else
				temp += "};\n";
		}
		
		writer.print(temp);
		writer.print("public static final int TransmitPeriod = " + transmitPeriod * 1000 + ";\n");
		writer.print("public static final int NumSensors = " + sensorNumber + ";\n");
		writer.print("public static final int HistoryLength = " + history + ";\n");
		
		//Read FWS1.java
		appendFile(".\\Generator\\codeBase/FWS1.java", writer);
		
		//DataBanker write
		//TODO: Need to be changed here
		for (Sensor s : sensors){
			writer.print("if (sensor.name.equals(\""+ s.name + "\")){\n");
			for (int i = 0; i < s.number; i++)
				writer.print("DataBanker.write(\"" + s.name + "\", SensorDriver" + (i+5) + ".get());\n");
			writer.print("}\n");
		}
		
		//Read FWS2.java
		appendFile(".\\Generator\\codeBase/FWS2.java", writer);
		
		//Read FWSShort.java or FWSLong.java
		if (messageFormat.equals("short"))
			appendFile(".\\Generator\\codeBase/FWSShort.java", writer);
		else
			appendFile(".\\Generator\\codeBase/FWSLong.java", writer);
		
		writer.close();
		
	}
	
	private static void appendFile(String in, PrintWriter writer){
		
		File file0 = new File(in);
		try {
			FileReader reader = new FileReader(file0);
			
			char[] buffer = new char[128];
			int size = 0;
			while((size = reader.read(buffer)) > 0){
				if (size == 128)
					writer.print(buffer);
				else
					for(int i = 0; i < size; i++)
						writer.print(buffer[i]);
			}
			reader.close();
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} 
	}
	
}

class Sensor{
	
	public String name;
	public int number;
	public int weight;
	public int period;
	
	public Sensor(String name, int number, int weight, int period){
		this.name = name;
		this.number = number;
		this.weight = weight;
		this.period = period;
	}
}
